package org.dailydev.mojo.birt;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.IOException;
import java.net.URLClassLoader;
import java.util.Arrays;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.classworlds.DefaultClassRealm;
import org.codehaus.classworlds.UberJarRealmClassLoader;
import org.codehaus.plexus.util.FileUtils;

/**
 * Goal which touches a timestamp file.
 * 
 * @goal report
 * 
 * @phase site
 */
public class BirtMojo extends AbstractMojo {

	public static String[] MODES = new String[] { "runrender", "run", "render" };

	public static String[] FORMATS = new String[] { "HTML", "PDF" };

	/**
	 * Reporting mode
	 * 
	 * @parameter expression="${birt.mode}" @default="runrender"
	 * @since 1.0
	 */
	private String mode = MODES[0];

	/**
	 * Reporting mode
	 * 
	 * @parameter expression="${birt.format}" @default="HTML"
	 * @since 1.0
	 */
	private String format = FORMATS[0];
	
	/**
	 * BIRT Runtime home
	 * 
	 * @parameter expression="${birt.format}" @default="HTML"
	 * @since 1.0
	 */
	private String birtHome;
	
	/**
	 * @parameter expression="${project}"
	 */
	private org.apache.maven.project.MavenProject mavenProject;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.maven.plugin.AbstractMojo#execute()
	 */
	public void execute() throws MojoExecutionException {
		getLog().info("Mode> " + mode);
		getLog().info("Format> " + format);

		// URLClassLoader classLoader = (URLClassLoader)
		// this.getClass().getClassLoader();

//		 new UberJarRealmClassLoader(new DefaultClassRealm())
		
		
		try {
			FileUtils.getFiles(new File("."), "", "");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// getLog().info("ClassLoader> " + classLoader);

//		getLog().info("Project> " + mavenProject);
//		getLog().info("Project.name> " + mavenProject.getName());
//		getLog().info("Dependencies> " + mavenProject.getCPlDependencies());
		
	}

	/**
	 * @return
	 * @throws MojoExecutionException
	 */
	public String getMode() throws MojoExecutionException {
		validateMode();
		return mode;
	}

	/**
	 * @param mode
	 * @throws MojoExecutionException
	 */
	public void setMode(String mode) throws MojoExecutionException {
		this.mode = mode;
		validateMode();
	}

	/**
	 * @throws MojoExecutionException
	 */
	protected void validateMode() throws MojoExecutionException {
		for (int i = 0; i < MODES.length; i++) {
			if (MODES[i].equalsIgnoreCase(mode)) {
				return;
			}
		}
		throw new MojoExecutionException("Parameter 'mode' should have one of following values: "
				+ Arrays.toString(MODES));
	}

	/**
	 * @return the format
	 * @throws MojoExecutionException
	 */
	public String getFormat() throws MojoExecutionException {
		validateFormat();
		return format;
	}

	/**
	 * @param format
	 *            the format to set
	 * @throws MojoExecutionException
	 */
	public void setFormat(String format) throws MojoExecutionException {
		this.format = format;
		validateFormat();
	}

	/**
	 * @throws MojoExecutionException
	 */
	protected void validateFormat() throws MojoExecutionException {
		for (int i = 0; i < FORMATS.length; i++) {
			if (FORMATS[i].equalsIgnoreCase(format)) {
				return;
			}
		}
		throw new MojoExecutionException("Parameter 'format' should have one of following values: "
				+ Arrays.toString(FORMATS));
	}
}
